//
//  Main_Library.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/10/16.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "MainSubLibraryView.h"

@interface MainSubLibraryView()

/// 模型视图
@property (nonatomic, readonly, strong) MainSubLibraryViewModel* viewModel;

//IBOutlet
@property (weak, nonatomic) IBOutlet UIView* contentView;

@end

@implementation MainSubLibraryView

- (void)bindViewModel:(id)viewModel
{
    MainSubLibraryViewModel* vm = viewModel;
    
    if (!self.viewModel)
    {
        _viewModel = vm;
        
        [self _setup];
    }
    else
    {
    }
}

- (void)_setup
{
    @weakify(self);
    
    [self.viewModel.reqDataCmd execute:nil];
    
    [[RACObserve(self.viewModel, articlesListData) filter:^BOOL(id value)
    {
        return value != nil;
    }] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self _reloadUI];
    }];
}

- (void)_reloadUI
{
    @weakify(self);
    
    [self.contentView removeAllSubviews];
    
    CGFloat offset = 12.0f;
    CGFloat width = (SCREEN_WIDTH - offset*3.0f)/2.0f;
    CGSize size = CGSizeMake(width, width);
    CGSize labelSize = CGSizeMake(size.width, size.height*0.35f);
    CGSize imgSize = CGSizeMake(size.width, size.height*0.65f);
    
    NSArray* categoryInfoDatas = self.viewModel.articlesListData.categorys;
    for (NSInteger index=0; index<categoryInfoDatas.count; index++)
    {
        NSInteger row = index/2;
        NSInteger tIndex = index%2;
        
        CategoryInfoData* categoryInfoData = categoryInfoDatas[index];
        
        UIView* subView = [[UIView alloc] initWithFrame:(CGRect) {offset + tIndex*(width+offset), offset + row*(width+offset), size}];
        subView.backgroundColor = UIColorHex(#e8e8e8);
        [self.contentView addSubview:subView];
        
        UIImageView* imgView = [[UIImageView alloc] initWithFrame:(CGRect) {0, 0, imgSize}];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [subView addSubview:imgView];

        UILabel* label = [[UILabel alloc] initWithFrame:(CGRect) {0, imgSize.height, labelSize}];
        label.text = categoryInfoData.cateName;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:16.0f];
        [subView addSubview:label];

        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = (CGRect) {0, 0, subView.frame.size};
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
        {
            @strongify(self);

            [self.viewModel.chooseCmd execute:categoryInfoData.cateID];
        }];
        [subView addSubview:btn];
    }
    
    NSInteger row = categoryInfoDatas.count/2;
    NSInteger tIndex = categoryInfoDatas.count%2;
    
    CGFloat contentViewHeight = (row + tIndex)*(size.height + offset) + offset;
    
    for (NSLayoutConstraint* curConstraint in self.contentView.constraints)
    {
        if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
        {
            curConstraint.constant = contentViewHeight;
            break;
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
